package com.aiiter.house.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.aiiter.common.utils.StringUtils;
import com.aiiter.house.domain.HouseChargeItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aiiter.house.mapper.HouseBillingInformationMapper;
import com.aiiter.house.domain.HouseBillingInformation;
import com.aiiter.house.service.IHouseBillingInformationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 存储账单的基本信息，包括房产、房主、租户的关联Service业务层处理
 * 
 * @author yaocheng
 * @date 2024-08-13
 */
@Service
public class HouseBillingInformationServiceImpl implements IHouseBillingInformationService 
{
    @Autowired
    private HouseBillingInformationMapper houseBillingInformationMapper;
    /**
     * 查询账单
     *
     * @param billId 账单主键
     * @return 账单
     */
    @Override
    public HouseBillingInformation selectHouseBillingInformationByBillId(Long billId)
    {
        return houseBillingInformationMapper.selectHouseBillingInformationByBillId(billId);
    }

    /**
     * 查询账单列表
     *
     * @param houseBillingInformation 账单
     * @return 账单
     */
    @Override
    public List<HouseBillingInformation> selectHouseBillingInformationList(HouseBillingInformation houseBillingInformation)
    {
        return houseBillingInformationMapper.selectHouseBillingInformationList(houseBillingInformation);
    }

    /**
     * 新增账单
     *
     * @param houseBillingInformation 账单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHouseBillingInformation(HouseBillingInformation houseBillingInformation)
    {
        int rows = houseBillingInformationMapper.insertHouseBillingInformation(houseBillingInformation);
        insertHouseChargeItem(houseBillingInformation);
        return rows;
    }

    /**
     * 修改账单
     *
     * @param houseBillingInformation 账单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateHouseBillingInformation(HouseBillingInformation houseBillingInformation)
    {
        houseBillingInformationMapper.deleteHouseChargeItemByBillId(houseBillingInformation.getBillId());
        insertHouseChargeItem(houseBillingInformation);
        return houseBillingInformationMapper.updateHouseBillingInformation(houseBillingInformation);
    }

    /**
     * 批量删除账单
     *
     * @param billIds 需要删除的账单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHouseBillingInformationByBillIds(Long[] billIds)
    {
        houseBillingInformationMapper.deleteHouseChargeItemByBillIds(billIds);
        return houseBillingInformationMapper.deleteHouseBillingInformationByBillIds(billIds);
    }

    /**
     * 删除账单信息
     *
     * @param billId 账单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHouseBillingInformationByBillId(Long billId)
    {
        houseBillingInformationMapper.deleteHouseChargeItemByBillId(billId);
        return houseBillingInformationMapper.deleteHouseBillingInformationByBillId(billId);
    }

    /**
     * 新增账单明细信息
     *
     * @param houseBillingInformation 账单对象
     */
    public void insertHouseChargeItem(HouseBillingInformation houseBillingInformation)
    {
        List<HouseChargeItem> houseChargeItemList = houseBillingInformation.getHouseChargeItemList();
        Long billId = houseBillingInformation.getBillId();
        if (StringUtils.isNotNull(houseChargeItemList))
        {
            List<HouseChargeItem> list = new ArrayList<HouseChargeItem>();
            for (HouseChargeItem houseChargeItem : houseChargeItemList)
            {
                houseChargeItem.setBillId(billId);
                list.add(houseChargeItem);
            }
            if (list.size() > 0)
            {
                houseBillingInformationMapper.batchHouseChargeItem(list);
            }
        }
    }
}
